package com.itheima.travel.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.github.pagehelper.PageInterceptor;
import com.itheima.travel.interceptor.PrimaryKeyInterceptor;
import org.apache.ibatis.logging.log4j2.Log4j2Impl;
import org.apache.ibatis.plugin.Interceptor;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;
import java.util.Properties;

@Configuration//配置类
@PropertySource("classpath:db.properties")
//类似于MapperScanner...
@MapperScan(basePackages ={ "com.itheima.travel.mapper",
        "com.itheima.travel.mapperExt"},
        sqlSessionFactoryRef = "sqlSessionFactoryBean")
public class MybatisConfig {
    @Value("${dataSource.driverClassName}")
    private String driver;
    @Value("${dataSource.url}")
    private String url;
    @Value("${dataSource.username}")
    private String username;
    @Value("${dataSource.password}")
    private String password;
    @Value("${seq.workerId}")
    private String workerId;
    @Value("${seq.datacenterId}")
    private String datacenterId;
    //创建数据源
    @Bean("druidDataSource")
    public DruidDataSource createDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }
    //创建事务
    @Bean("transactionManager")
    public DataSourceTransactionManager createTransactionManager(@Qualifier("druidDataSource") DataSource dataSource ){
        return new DataSourceTransactionManager(dataSource);
    }
    @Bean("sqlSessionFactoryBean")
    public SqlSessionFactoryBean sqlSessionFactoryBean( DataSource druidDataSource){
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        //指定数据源
        sqlSessionFactoryBean.setDataSource(druidDataSource);
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        configuration.setLogImpl(Log4j2Impl.class);
        sqlSessionFactoryBean.setConfiguration(configuration);
        //配置分页插件
        sqlSessionFactoryBean.setPlugins(new Interceptor[]{initPageInterceptor(),initPrimaryKeyInterceptor()});
        return sqlSessionFactoryBean;
    }

    //雪花算法支持
    @Bean
    public SnowflakeIdWorker snowflakeIdWorker(){
        return new SnowflakeIdWorker(Integer.valueOf(workerId), Integer.valueOf(datacenterId));
    }
    //生成拦截器
    @Bean
    public PrimaryKeyInterceptor initPrimaryKeyInterceptor(){
        // 创建拦截器对象
        PrimaryKeyInterceptor primaryKeyInterceptor =  new PrimaryKeyInterceptor();
        // 设置拦截器的相关参数信息
        Properties properties = new Properties();
        // primaryKey: 自定义的键名称
        // id: 需要替换的字段
        properties.setProperty("primaryKey", "id");
        primaryKeyInterceptor.setProperties(properties);
        // 给主键生成拦截器传入雪花算法工具类
        primaryKeyInterceptor.setSnowflakeIdWorker(snowflakeIdWorker());
        return primaryKeyInterceptor;
    }

    //配置分页插件信息
    @Bean
    public Interceptor initPageInterceptor() {
        PageInterceptor pageInterceptor = new PageInterceptor();
        Properties properties = new Properties();
        properties.setProperty("helperDialect", "mysql");
        properties.setProperty("offsetAsPageNum", "true");
        properties.setProperty("rowBoundsWithCount", "true");
        pageInterceptor.setProperties(properties);
        return pageInterceptor;
    }
}
